package web.suzy.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.suzy.oj.common.result.ResultBean;
import web.suzy.oj.pojo.dto.AdminEditUserDTO;
import web.suzy.oj.pojo.vo.SysUserRoleVO;
import web.suzy.oj.service.admin.user.AdminUserService;

import java.util.List;
import java.util.Map;

/**
 * YangSuzy 软件工作室
 * 类名: AdminUserController
 * 描述: ---- 待定 -----
 * 功能: 处理管理员对用户的增删改查操作
 *
 * @author YangSuzy
 * Date: 2022/11/17 19:10
 */
@RestController
@RequestMapping("/api/admin/user")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    /**
     * 方法名: getUserList
     * 描述: 根据关键字分页查询用户列表
     *
     * @param limit       每页显示数量
     * @param currentPage 当前页
     * @param onlyAdmin   是否只查管理员
     * @param keyword     关键字
     * @return web.suzy.oj.common.result.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < web.suzy.oj.pojo.vo.SysUserRoleVO>>
     * @date 2022/11/17 19:12
     * @auther YangSuzy
     **/
    @GetMapping("/get-user-list")
    @RequiresAuthentication
    @RequiresPermissions("user_admin")
    public ResultBean<IPage<SysUserRoleVO>> getUserList(@RequestParam(value = "limit", required = false) Integer limit,
                                                        @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                        @RequestParam(value = "onlyAdmin", defaultValue = "false") Boolean onlyAdmin,
                                                        @RequestParam(value = "keyword", required = false) String keyword) {
        return adminUserService.getUserList(limit, currentPage, onlyAdmin, keyword);
    }

    /**
     * 方法名: deleteUser
     * 描述: 批量删除用户
     *
     * @param params 待删除用户ID列表
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/17 22:15
     * @auther YangSuzy
     **/
    @DeleteMapping("/delete-user")
    @RequiresAuthentication
    @RequiresPermissions("user_admin")
    public ResultBean<Void> deleteUser(@RequestBody Map<String, Object> params) {
        return adminUserService.deleteUser((List<String>) params.get("ids"));
    }

    /**
     * 方法名: editUser
     * 描述: 管理员修改用户信息
     *
     * @param adminEditUserDto 管理员修改用户数据传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/27 18:33
     * @auther YangSuzy
     **/
    @PutMapping("/edit-user")
    @RequiresPermissions("user_admin")
    @RequiresAuthentication
    public ResultBean<Void> editUser(@RequestBody AdminEditUserDTO adminEditUserDto) {
        return adminUserService.editUser(adminEditUserDto);
    }

    /**
     * 方法名: generateUser
     * 描述: 快速生成用户 用户名：前缀 + 数字 + 后缀 随机6位密码
     *
     * @param params 用户数据参数
     * @return web.suzy.oj.common.result.ResultBean<java.util.Map < java.lang.Object, java.lang.Object>>
     * @date 2022/11/27 17:06
     * @auther YangSuzy
     **/
    @PostMapping("/generate-user")
    @RequiresPermissions("user_admin")
    @RequiresAuthentication
    public ResultBean<Map<Object, Object>> generateUser(@RequestBody Map<String, Object> params) {
        return adminUserService.generateUser(params);
    }

    /**
     * 方法名: insertBatchUser
     * 描述: 通过CSV文件批量导入用户
     *
     * @param params 用户数据参数
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/27 19:06
     * @auther YangSuzy
     **/
    @PostMapping("/insert-batch-user")
    @RequiresPermissions("user_admin")
    @RequiresAuthentication
    public ResultBean<Void> insertBatchUser(@RequestBody Map<String, Object> params) {
        return adminUserService.insertBatchUser((List<List<String>>) params.get("users"));
    }
}
